package com.cxrj.zhsq.partyMember.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.partyMember.dao.PartyMemberMapper;
import com.cxrj.zhsq.partyMember.entity.PartyMember;
import com.cxrj.zhsq.partyMember.service.PartyMemberService;

@Service
public class PartyMemberServiceImpl implements PartyMemberService {
	
	@Resource
	private PartyMemberMapper partyMemberMapper;

	@Override
	public Map<String, Object> insertAndUpdatePartyMember(PartyMember partyMember) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			if ("".equals(partyMember.getPartyMemberId())) {
				partyMember.setPartyMemberId(UUID.randomUUID().toString());
				partyMember.setCreateTime(new Date());
				partyMemberMapper.insertSelective(partyMember);
				
			}else{
				partyMemberMapper.updateByPrimaryKeySelective(partyMember);
			}
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> selectPartyMemberPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Map<String, Object> sqlMap=new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		//数据起始位置
		String start=request.getParameter("offset");
		//数据长度
		String length=request.getParameter("limit");
		//总条数
		String recordsTotal = "0";
		//查询条件
		String kod=request.getParameter("kod");
		String sqlStr = " where 1=1 ";
		
		if(kod != null && !kod.equals("")){
			sqlStr += " and ( zpm.party_member_name like " + '"' + '%' + kod + '%' + '"'+" or zpm.Id_number like " + '"' + '%' + kod + '%' + '"'+" or zpm.unit like " + '"' + '%' + kod + '%' + '"'+')';
		}
		
		recordsTotal=partyMemberMapper.getMemberCount(sqlStr)+"";
		sqlMap.put("condition",sqlStr);
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		List<Map<String, Object>> noticeList=partyMemberMapper.selectPartyMemberPage(sqlMap);
		
		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", noticeList);
		
		return resultMap;
	}

	@Override
	public Map<String, Object> selectById(String partyMemberId) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			PartyMember partyMember=partyMemberMapper.selectByPrimaryKey(partyMemberId);
			resultMap.put("PartyMember", partyMember);
		} catch (Exception e) {
			resultMap.put("PartyMember", "");
			e.printStackTrace();
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> deleteMember(String partyMemberId) {
		Map<String, Object> resultMap=new HashMap<>();
		try {
			partyMemberMapper.deleteByPrimaryKey(partyMemberId);
			resultMap.put("result", "1");
		} catch (Exception e) {
			resultMap.put("result", "-1");
			e.printStackTrace();
		}
		return resultMap;
	}

}
